package com.newly.center.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newly.center.business.service.GoodsTypeService;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.business.Po.GoodsTypePo;
import com.newly.common.base.exception.NewlyException;
import com.newly.common.mapper.business.GoodsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chenruheng
 * @Date: 2022/2/7 15:49
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypePo> implements GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public PageUtils listGoodsType(Integer offset, Integer limit, String name, String startTime, String endTime) {
        IPage<GoodsTypePo> page = goodsTypeMapper.selectPage(
                new Page<>(offset, limit),
                Wrappers.lambdaQuery(GoodsTypePo.class)
                        .like(StrUtil.isNotBlank(name), GoodsTypePo::getName, name)
                        .between(StrUtil.isAllNotBlank(startTime, endTime),
                                GoodsTypePo::getCreateTime, startTime, endTime)
        );

        return new PageUtils(page);
    }

    @Override
    public boolean createGoodsType(ICurrentTenant currentTenant, String goodsType) {
        if (StrUtil.isBlank(goodsType)) {
            throw new NewlyException(CodeConstant.FAILED, "货物类型为空");
        }
        DateTime date = DateUtil.date();
        GoodsTypePo po = new GoodsTypePo()
                .setCreateTenantId(currentTenant.getUserId())
                .setCreateTime(date)
                .setUpdateTime(date)
                .setName(goodsType);

        int insert = 0;
        try {
            insert = goodsTypeMapper.insert(po);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            if (msg.contains("Duplicate")) {
                throw new NewlyException(CodeConstant.FAILED, "该货物类型已存在");
            }
        }
        return insert > 0;
    }

    @Override
    public List<String> getGoodsType(String goodsType) {
        List<String> list = goodsTypeMapper.selectList(
                Wrappers.lambdaQuery(GoodsTypePo.class)
                        .select(GoodsTypePo::getName)
                        .like(StrUtil.isNotBlank(goodsType), GoodsTypePo::getName, goodsType)
        ).stream()
                .map(GoodsTypePo::getName).collect(Collectors.toList());

        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        return list;
    }

    @Override
    public boolean delGoodsType(List<String> ids) {
        if (ObjectUtil.isEmpty(ids)){
            throw new NewlyException(CodeConstant.FAILED,"ids为空");
        }

        int result;
        try {
            result = goodsTypeMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new NewlyException(CodeConstant.FAILED,"删除货物类型失败");
        }
        return result > 0;
    }

}
